furny.furndb
Class FurnDBManager

java.lang.Object
  extended by furny.furndb.FurnDBManager

public final class FurnDBManager
extends java.lang.Object

Class that manages furniture database accesses.
This class is a SINGLETON.

Since:
10.08.2012
Author:
Stephan Dreyer

Field Summary
private  com.jme3.asset.AssetManager assetManager
           
private  javax.persistence.EntityManagerFactory entityManagerFactory
           
private  java.util.concurrent.ExecutorService executor
           
private  java.util.List<FurnitureUpdateListener> furnitureListeners
           
private static FurnDBManager INSTANCE
           
private static java.util.logging.Logger LOGGER
           
private static java.lang.String PARAMETERS
           
private  java.util.List<TagUpdateListener> tagListeners
           
private  java.util.List<java.lang.Long> updateIds
           
 
Constructor Summary
private FurnDBManager()
          Private constructor for single instance.
 
Method Summary
 void addFurnitureTags(Furniture furniture, java.util.Set<Tag> newTags)
          Adds a new set of tags to an existing furniture.
 void addFurnitureUpdateListener(FurnitureUpdateListener listener)
          Adds a FurnitureUpdateListener that will be notified about changes.
 void addTagUpdateListener(TagUpdateListener listener)
          Adds a TagUpdateListener that will be notified about changes.
private  boolean convertTexturesToDB(javax.persistence.EntityManager entityManager, Furniture f, java.io.File path)
          Helper method that creates byte arrays from textures to save them in the database.
private  javax.persistence.EntityManager createEntityManager()
          Creates an EntityManager to start a database transaction.
 void deleteFurniture(java.lang.Long id)
          Deletes a furniture from the database.
 void deleteTag(Tag tag)
          Deletes a tag from the database.
 void exit()
          Lets the manager exit and clean up its resources.
private  void fireFurnitureDeleted(java.lang.Long id)
          Informs the listeners about an tag update.
private  void fireFurnitureUpdate(java.lang.Long id, Furniture furniture)
          Informs the listeners about an furniture update.
 com.jme3.asset.AssetManager getAssetManager()
          Getter for the asset manager.
 Furniture getFurniture(java.lang.Long id)
          Gets a furniture by id.
 java.util.List<java.lang.Long> getFurnitureIds()
          Requests all furniture ids from the database.
private  java.util.List<java.lang.Long> getFurnitureIdsByString(SearchType searchType, java.lang.String text, boolean equals)
          Finds furnitures by the given string and the type of the search to be performed.
 java.util.List<Furniture> getFurnitures()
          Requests all furnitures from the database.
static FurnDBManager getInstance()
          Getter for the single instance.
private  java.util.List<Tag> getTags(TagType type)
          Gets all tags of the given type.
 TextureData getTextureData(java.lang.String key)
          Loads texture data for a model from the database.
 void removeTagUpdateListener(TagUpdateListener listener)
          Removes a TagUpdateListener, so it will not be notified anymore.
 void saveFurniture(Furniture furniture, boolean merge)
          Saves a furniture to the database.
 void saveFurniture(Furniture furniture, boolean merge, java.io.File path)
          Saves a furniture to the database.
 void saveTag(Tag tag)
          Saves a new tag.
 void setCustomFilename(java.lang.String filename)
          Sets a custom file name for the database.
 void setFurnitureTags(Furniture furniture, java.util.Set<Tag> tags)
          Sets a new set of tags for the existing furniture.
 void updateFurniture(java.lang.Long id)
          Non-blocking method to get a furniture by id.
 void updateFurnitureIds()
          Non-blocking method to get the furniture ids.
 void updateIdsByTagName(SearchType searchType, java.lang.String text, boolean equals)
          Non-blocking method to get find furnitures by a string.
 void updateTags(TagType type)
          Non-blocking method to get tags by their type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER

INSTANCE

private static final FurnDBManager INSTANCE

PARAMETERS

private static final java.lang.String PARAMETERS
See Also:
Constant Field Values

entityManagerFactory

private javax.persistence.EntityManagerFactory entityManagerFactory

assetManager

private final com.jme3.asset.AssetManager assetManager

furnitureListeners

private final java.util.List<FurnitureUpdateListener> furnitureListeners

tagListeners

private final java.util.List<TagUpdateListener> tagListeners

executor

private final java.util.concurrent.ExecutorService executor

updateIds

private final java.util.List<java.lang.Long> updateIds
Constructor Detail

FurnDBManager

private FurnDBManager()
Private constructor for single instance.

Since:
10.08.2012
Method Detail

getInstance

public static FurnDBManager getInstance()
Getter for the single instance.

Returns:
Single instance of this manager.
Since:
10.08.2012

setCustomFilename

public void setCustomFilename(java.lang.String filename)
Sets a custom file name for the database.

Parameters:
filename - Furniture database file name.
Since:
10.08.2012

getAssetManager

public com.jme3.asset.AssetManager getAssetManager()
Getter for the asset manager.

Returns:
Asset manager.
Since:
10.08.2012

createEntityManager

private javax.persistence.EntityManager createEntityManager()
Creates an EntityManager to start a database transaction.

Returns:
A new entitity manager.
Since:
10.08.2012

saveFurniture

public void saveFurniture(Furniture furniture,
                          boolean merge)
Saves a furniture to the database. Merge must be true if the furniture already exists and needs only an update, otherwise it will be created.

Parameters:
furniture - Furniture to save
merge - Merge (update) or create (insert)
Since:
10.08.2012

saveFurniture

public void saveFurniture(Furniture furniture,
                          boolean merge,
                          java.io.File path)
Saves a furniture to the database. Merge must be true if the furniture already exists and needs only an update, otherwise it will be created.

Parameters:
furniture - Furniture to save
merge - Merge (update) or create (insert)
path - The path where this model has been loaded.
Since:
10.08.2012

convertTexturesToDB

private boolean convertTexturesToDB(javax.persistence.EntityManager entityManager,
                                    Furniture f,
                                    java.io.File path)
                             throws java.io.IOException
Helper method that creates byte arrays from textures to save them in the database.

Parameters:
entityManager - The open EntityManager.
f - The furniture that contains the textures.
path - The path where the textures are.
Returns:
true if textures were converted and the furniture needs to be re-saved.
Throws:
java.io.IOException - if byte array conversion fails
Since:
10.08.2012

saveTag

public void saveTag(Tag tag)
Saves a new tag. The tag will not be merged, if it already exists!

Parameters:
tag - The tag to save.
Since:
10.08.2012

deleteFurniture

public void deleteFurniture(java.lang.Long id)
Deletes a furniture from the database.

Parameters:
id - Id of the furniture that should be deleted
Since:
10.08.2012

deleteTag

public void deleteTag(Tag tag)
Deletes a tag from the database.

Parameters:
tag - Tag to delete.
Since:
10.08.2012

getFurnitureIds

public java.util.List<java.lang.Long> getFurnitureIds()
Requests all furniture ids from the database.

Returns:
A list of all furniture ids.
Since:
10.08.2012

getFurniture

public Furniture getFurniture(java.lang.Long id)
Gets a furniture by id.

Parameters:
id - Id of the furniture to get.
Returns:
The furniture.
Since:
10.08.2012

getTextureData

public TextureData getTextureData(java.lang.String key)
Loads texture data for a model from the database.

Parameters:
key - The key of the texture.
Returns:
The texture data.
Since:
10.08.2012

getFurnitures

public java.util.List<Furniture> getFurnitures()
Requests all furnitures from the database.

Returns:
A list of all furnitures.
Since:
10.08.2012

getTags

private java.util.List<Tag> getTags(TagType type)
Gets all tags of the given type.

Parameters:
type - Type to find.
Returns:
All tags for that type.
Since:
10.08.2012

addFurnitureTags

public void addFurnitureTags(Furniture furniture,
                             java.util.Set<Tag> newTags)
Adds a new set of tags to an existing furniture.

Parameters:
furniture - The furniture.
newTags - The set of tags to add.
Since:
10.08.2012

setFurnitureTags

public void setFurnitureTags(Furniture furniture,
                             java.util.Set<Tag> tags)
Sets a new set of tags for the existing furniture. The old tags will be replaced!

Parameters:
furniture - The furniture.
tags - The set of tags to set.
Since:
10.08.2012

getFurnitureIdsByString

private java.util.List<java.lang.Long> getFurnitureIdsByString(SearchType searchType,
                                                               java.lang.String text,
                                                               boolean equals)
Finds furnitures by the given string and the type of the search to be performed. Returns the ids of the results.

Parameters:
searchType - Type of search.
text - String to search for.
equals - If true the String must equal, otherwise it must be contained.
Returns:
A list of ids of the search result.
Since:
10.08.2012

updateFurnitureIds

public void updateFurnitureIds()
Non-blocking method to get the furniture ids. This requires the client to register an FurnitureUpdateListener. The listener will be notified about the ids.

Since:
10.08.2012
See Also:
getFurnitureIds()

updateTags

public void updateTags(TagType type)
Non-blocking method to get tags by their type. This requires the client to register an TagUpdateListener. The listener will be notified about the tags.

Parameters:
type - Type to find.
Since:
10.08.2012
See Also:
getTags(TagType)

updateFurniture

public void updateFurniture(java.lang.Long id)
Non-blocking method to get a furniture by id. This requires the client to register an FurnitureUpdateListener. The listener will be notified about the furniture.

Parameters:
id - Id of the furniture to get.
Since:
10.08.2012
See Also:
getFurniture(Long)

updateIdsByTagName

public void updateIdsByTagName(SearchType searchType,
                               java.lang.String text,
                               boolean equals)
Non-blocking method to get find furnitures by a string. This requires the client to register an FurnitureUpdateListener. The listener will be notified about the furnitures.

Parameters:
searchType - Type of search.
text - String to search for.
equals - If true the String must equal, otherwise it must be contained.
Since:
10.08.2012
See Also:
getFurniture(Long)

addFurnitureUpdateListener

public void addFurnitureUpdateListener(FurnitureUpdateListener listener)
Adds a FurnitureUpdateListener that will be notified about changes.

Parameters:
listener - Listener to add.
Since:
10.08.2012

addTagUpdateListener

public void addTagUpdateListener(TagUpdateListener listener)
Adds a TagUpdateListener that will be notified about changes.

Parameters:
listener - Listener to add.
Since:
10.08.2012

removeTagUpdateListener

public void removeTagUpdateListener(TagUpdateListener listener)
Removes a TagUpdateListener, so it will not be notified anymore.

Parameters:
listener - Listener to remove.
Since:
10.08.2012

fireFurnitureUpdate

private void fireFurnitureUpdate(java.lang.Long id,
                                 Furniture furniture)
Informs the listeners about an furniture update.

Parameters:
id - Changed id
furniture - Changed furniture
Since:
10.08.2012

fireFurnitureDeleted

private void fireFurnitureDeleted(java.lang.Long id)
Informs the listeners about an tag update.

Parameters:
id - Changed id
Since:
10.08.2012

exit

public void exit()
Lets the manager exit and clean up its resources. It will not be reusable after that!

Since:
10.08.2012